unit BD_Config;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, StrUtils;

type
  TfrmConfig = class(TForm)
    sgConfig: TStringGrid;
    btnOK: TButton;
    btnCancel: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    ParamList: TStringList;
  end;

var
  frmConfig: TfrmConfig;

implementation

{$R *.dfm}

uses BDPgmCalMTMain, BD_PgmCalMT_Aux;

procedure TfrmConfig.btnOKClick(Sender: TObject);
var
  i, idx: Integer; Changed: Boolean;
begin
  Changed := False;
  for i := 0 to ParamList.Count-1 do
    begin
      idx := PosEx(',',ParamList[i],1);     //find the comma separator
      //If any cell has changed, set Changed flag to true
      if copy(ParamList[i],idx+1, Length(ParamList[i])-idx) <> sgConfig.Cells[1,i] then
        Changed := True;
    end;
  if Changed then
    if MessageDlg('Config file parameter(s) have changed- save changes?',
                   mtInformation,[mbOK,mbCancel],0) = 1 then
      begin
        ParamList.Clear;      //clear all values
        for i := 0 to sgConfig.RowCount-1 do
          ParamList.Add(sgConfig.Cells[0,i] + ', '+ sgConfig.Cells[1,i]);
        try
          ParamList.SaveToFile(ExtractFilePath(Application.ExeName)+'BD_Config.txt');
          LoadCfgParams(cfgParams);   //initiallize the parameters from the cfg file.
        except
          ShowMessage('Error occured when saving config file changes.');
        end;
      end;
end;

procedure TfrmConfig.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Assigned(ParamList) then
    ParamList.Free;
end;

procedure TfrmConfig.FormCreate(Sender: TObject);
var
  i, idx, MaxWidth: Integer;
begin
  ParamList := TStringList.Create;
  MaxWidth := 0;
  if FileExists(ExtractFilePath(Application.ExeName)+'BD_Config.txt') then
    try
      ParamList.LoadFromFile(ExtractFilePath(Application.ExeName)+'BD_Config.txt');
      sgConfig.RowCount := ParamList.Count;
      for i := 0 to ParamList.Count-1 do
        begin
          idx := PosEx(',',ParamList[i],1);     //find the comma separator
          sgConfig.Cells[0,i] := copy(ParamList[i],1,idx-1);
          sgConfig.Cells[1,i] := copy(ParamList[i],idx+1, Length(ParamList[i])-idx);
          if sgConfig.Canvas.TextWidth(sgConfig.Cells[0,i]) > MaxWidth then
        MaxWidth := sgConfig.Canvas.TextWidth(sgConfig.Cells[0,i]);
        end;
    except
      ShowMessage('Unable to open configuration file.');
      ParamList.Free;
      Self.Close;
    end
  else
    begin
      ShowMessage('Unable to find configuration file.');
      ParamList.Free;
      Self.Close;
    end;
  //now adjust the column widths to looks nice

  //Now set the column 0 (parameter lables) width the match the widest parameter string
  sgConfig.ColWidths[0] := MaxWidth + 28;
  sgConfig.ColWidths[1] := sgConfig.Width - sgConfig.ColWidths[0] - 4;
  //sgConfig.Height := (sgConfig.RowCount * sgConfig.RowHeights[0]) +
                                   // (sgConfig.RowCount * 1);
end;

end.
